const info = require('../../providers/dataAccess').info;
const sys = require('../../providers/dataAccess').sys;
const shop = require('../../providers/dataAccess').shop;
const apiStatus = require('../../apiStatus');

const SlideshowsService = function () {
    return {
        updateCategory: async(ctx,next) =>{
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;
            let categoryName = ctx.request.body.categoryName;
            if (categoryId > 0){
                let szSql = 'UPDATE app_slideshow_categories SET categoryName = ? WHERE appId = ? AND id = ?;';
                await info.query(szSql,[categoryName,appId,categoryId]);
            }else{
                let szSql = 'INSERT INTO app_slideshow_categories(appId, categoryName) VALUES(?,?);';
                await info.query(szSql,[appId,categoryName]);
            }

            ctx.apiStatus = apiStatus.success;
            next();

        },
        removeCategory: async(ctx,next) =>{
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;

            let szSql = `
                DELETE FROM app_slideshows WHERE appId= ? AND categoryId = ?;
                DELETE FROM app_slideshow_categories WHERE appId = ? AND id = ?;
            `;
            await info.query(szSql,[appId,categoryId,appId,categoryId]);

            ctx.apiStatus = apiStatus.success;
            next();
        },
        categories: async(ctx,next) =>{
            let appId = ctx.appInfo.id;
            let szSql = 'SELECT * FROM app_slideshow_categories WHERE appId = ?';
            let rows = await info.query(szSql,[appId]);

            for(var i = 0; i < rows.length; i++){
                szSql = "SELECT * FROM app_slideshows WHERE appId = ? AND categoryId = ? ORDER BY sequence";
                rows[i].slideshows = await info.query(szSql, [appId, rows[i].id]);
            }

            ctx.apiBody = rows;
            next();
        },
        slideshows: async(ctx,next) => {
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;
            let szSql = "SELECT * FROM app_slideshows WHERE appId = ? AND categoryId = ? ORDER BY sequence";
            let rows = await info.query(szSql,[appId,categoryId]);
            ctx.apiBody = rows;
            next();
        },
        addSlideshow: async(ctx, next) => {
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;
            let imageUrl = ctx.request.body.imageUrl;
            let szSql = `
                INSERT INTO app_slideshows (appId, categoryId, sequence, title, image)
                SELECT ?, ?, IFNULL(MAX(sequence), 0) + 1, ?, ?
                FROM app_slideshows
                WHERE appId = ? AND categoryId = ?
            `;
            await info.query(szSql,[appId,categoryId,'Noname',imageUrl, appId, categoryId]);
            ctx.apiStatus = apiStatus.success;
            next();
        },
        updateSlideshow: async(ctx, next) => {
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;
            let slideshowId = ctx.request.body.slideshowId;
            let title = ctx.request.body.title;
            let szSql = `
                UPDATE app_slideshows SET title = ?
                WHERE appId = ? AND categoryId = ? AND id = ?
            `;
            await info.query(szSql,[title, appId, categoryId, slideshowId]);
            ctx.apiStatus = apiStatus.success;
            next();
        },
        removeSlideshow: async(ctx, next) => {
            let appId = ctx.appInfo.id;
            let categoryId = ctx.request.body.categoryId;
            let slideshowId = ctx.request.body.slideshowId;
            let szSql = `
                DELETE FROM app_slideshows WHERE appId = ? AND categoryId = ? AND id = ?
            `;
            await info.query(szSql,[appId, categoryId, slideshowId]);
            ctx.apiStatus = apiStatus.success;
            next();
        }

    }
}();

module.exports = SlideshowsService;
